﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Otgruzka.ServicesClients;
using BusinessLogic.Security;
using System.ServiceModel;
using DataTier;

namespace Otgruzka.Controls.PromptMessages
{
    public partial class PromptMessageEdit : System.Web.UI.UserControl
    {
        public int PromptId
        {
            get
            {
                int returned = 0;
                if ((!string.IsNullOrEmpty(this.hPromptMessageId.Value)))
                {
                    try
                    {
                        returned = Convert.ToInt32(this.hPromptMessageId.Value);
                    }
                    catch (Exception ex)
                    { }
                }
                return returned;
            }
            set
            {
                this.hPromptMessageId.Value = value.ToString();
                SetVisibilityButtons();
            }
        }

        public int Number
        {
            get
            {
                int returned = 0;
                if ((!string.IsNullOrEmpty(this.hPromptMessageNumber.Value)))
                {
                    try
                    {
                        returned = Convert.ToInt32(this.hPromptMessageNumber.Value);
                    }
                    catch (Exception ex)
                    { }
                }
                return returned;
            }
            set
            {
                this.hPromptMessageNumber.Value = value.ToString();
            }
        }

        public string ForPage
        {
            get { return this.hForPage.Value; }
            set { this.hForPage.Value = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindWebPageListData();
            }
            SetVisibilityButtons();
        }


        protected void listWebPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPromptIndexes();
            ClearForm();
        }

        protected void listPromptIndexes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listPromptIndexes.SelectedItem != null && !string.IsNullOrEmpty(listPromptIndexes.SelectedItem.Value))
                this.PromptId = Convert.ToInt32(listPromptIndexes.SelectedItem.Value);
            if (listPromptIndexes.SelectedItem != null && !string.IsNullOrEmpty(listPromptIndexes.SelectedItem.Text))
                this.Number = Convert.ToInt32(listPromptIndexes.SelectedItem.Text);

            BindPromptMessage();
        }


        /// <summary>
        /// Заполнение списка страниц системы
        /// </summary>
        void BindWebPageListData()
        {
            var client = new DictionariesClient();
            try
            {
                //список страниц системы
                var list = client.GetWebPageASPXList(UserPrincipal.Current.UserId);
                listWebPage.DataSource = list;
                listWebPage.DataBind();
            }
            catch (System.ServiceModel.FaultException<ExceptionDetail> ex)
            {
                string message = client.GetExceptionDetail(ex);
                this.ErrorMessage.Text = message;
            }
            catch (System.Exception exp)
            {
                this.ErrorMessage.Text = exp.Message;
            }
        }


        private void BindPromptIndexes()
        {
            if (listWebPage.SelectedItem != null && !string.IsNullOrEmpty(listWebPage.SelectedItem.Text))
            {
                this.ForPage = listWebPage.SelectedItem.Text;
                var client = new PromptMessagesClient();
                try
                {
                    var messages = client.GetPromptMessageList(this.ForPage);
                    listPromptIndexes.DataSource = messages;
                    listPromptIndexes.DataBind();
                }
                catch (System.ServiceModel.FaultException<ExceptionDetail> ex)
                {
                    string msg = client.GetExceptionDetail(ex);
                    this.ErrorMessage.Text = msg;
                }
                catch (System.Exception exp)
                {
                    this.ErrorMessage.Text = exp.Message;
                }
            }
        }

        /// <summary>
        /// Заполнение текста подсказки
        /// </summary>
        void BindPromptMessage()
        {
            var client = new PromptMessagesClient();
            try
            {
                var message = client.GetPromptMessage(UserPrincipal.Current.UserId, PromptId);
                promptHtml.Text = message.Message;
            }
            catch (System.ServiceModel.FaultException<ExceptionDetail> ex)
            {
                string msg = client.GetExceptionDetail(ex);
                this.ErrorMessage.Text = msg;
            }
            catch (System.Exception exp)
            {
                this.ErrorMessage.Text = exp.Message;
            }
        }


        protected void newPromptMessage_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void ClearForm()
        {
            this.PromptId = 0;
            this.Number = 0;
            if (listWebPage.SelectedItem != null && !string.IsNullOrEmpty(listWebPage.SelectedItem.Text))
                this.ForPage = listWebPage.SelectedItem.Text;
            listPromptIndexes.ClearSelection();
            promptHtml.Text = "";
        }


        protected void savePromptMessage_Click(object sender, EventArgs e)
        {
            var message = GetPromptingMessage();

            if (message != null)
            {
                var client = new PromptMessagesClient();
                try
                {
                    int id = client.SavePromptMessage(UserPrincipal.Current.UserId, message);
                    //this.PromptId = id;
                    BindPromptIndexes();
                    ClearForm();
                }
                catch (System.ServiceModel.FaultException<ExceptionDetail> ex)
                {
                    string msg = client.GetExceptionDetail(ex);
                    this.ErrorMessage.Text = msg;
                }
                catch (System.Exception exp)
                {
                    this.ErrorMessage.Text = exp.Message;
                }
            }
        }


        protected void deletePromptMessage_Click(object sender, EventArgs e)
        {
                var client = new PromptMessagesClient();
                try
                {
                    client.DeletePromptMessage(UserPrincipal.Current.UserId, this.PromptId);
                    BindPromptIndexes();
                    ClearForm();
                }
                catch (System.ServiceModel.FaultException<ExceptionDetail> ex)
                {
                    string msg = client.GetExceptionDetail(ex);
                    this.ErrorMessage.Text = msg;
                }
                catch (System.Exception exp)
                {
                    this.ErrorMessage.Text = exp.Message;
                }
        }


        private void SetVisibilityButtons()
        {
            deletePromptMessage.Visible = (PromptId > 0);
        }


        private PromptingMessage GetPromptingMessage()
        {
            string htmltext = promptHtml.Text;

            var message = new PromptingMessage()
            {
                PromptId = this.PromptId,
                Number = this.Number,
                Message = htmltext,
                ForPage = this.ForPage
            };

            return message;
        }

    }
}